package com.snails.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.snails.ui.R;

public class LoadingDialog extends Dialog
        implements View.OnClickListener, DialogInterface.OnCancelListener {
    private Context mCtx = null;
    private ImageView mLoadingImg;
    private TextView mLoadingTxt, mLoadingBtnCancel;
    private AnimationDrawable mLoadingDrawable;

    private String message = "", button = "";
    private OnLoadingListener listener = null;

    public LoadingDialog(@NonNull Context context, Builder builder) {
        super(context, R.style.CommonDialog);
        this.mCtx = context;
        this.message = builder.message;
        this.button = builder.button;
        this.listener = builder.listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_loading);
        findUi().setUi().setOnClick();
        setCancelable(true);
        setCanceledOnTouchOutside(false);
    }

    private LoadingDialog findUi() {
        mLoadingImg = findViewById(R.id.loading_anim);
        mLoadingDrawable = (AnimationDrawable) mLoadingImg.getDrawable();
        mLoadingTxt = findViewById(R.id.loading_txt);
        mLoadingBtnCancel = findViewById(R.id.loading_cancel);
        return this;
    }

    private LoadingDialog setUi() {
        setMessage(message);

        mLoadingBtnCancel.setText(button);
        mLoadingBtnCancel.setVisibility(TextUtils.isEmpty(button) ? View.GONE : View.VISIBLE);

        return this;
    }

    public LoadingDialog setMessage(String message) {
        mLoadingTxt.setText(message);
        mLoadingTxt.setVisibility(TextUtils.isEmpty(message) ? View.GONE : View.VISIBLE);
        return this;
    }

    private LoadingDialog setOnClick() {
        this.setOnCancelListener(this);
        mLoadingBtnCancel.setOnClickListener(this);
        return this;
    }

    @Override
    public void show() {
        super.show();
        mLoadingDrawable.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mLoadingDrawable.stop();
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (listener != null) {
            listener.onClick();
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (listener != null) {
            listener.onClick();
        }
    }

    // **************************************************************************
    // ===== 构造器 =====
    // **************************************************************************
    public static class Builder {
        private String message = "";
        private String button = "";
        private OnLoadingListener listener = null;

        /**
         * 提示文本
         */
        public Builder message(String message) {
            this.message = message;
            return this;
        }

        /**
         * 提示文本
         */
        public Builder message(Context ctx, @StringRes int message) {
            this.message = ctx.getString(message);
            return this;
        }

        /**
         * 按钮文本
         */
        public Builder button(String button, OnLoadingListener listener) {
            this.button = button;
            this.listener = listener;
            return this;
        }

        /**
         * 按钮文本
         */
        public Builder button(Context ctx, @StringRes int button, OnLoadingListener listener) {
            this.button = ctx.getString(button);
            this.listener = listener;
            return this;
        }

        public LoadingDialog build(Context ctx) {
            return new LoadingDialog(ctx, this);
        }
    }

    // **************************************************************************
    // ===== 事件监听器 =====
    // **************************************************************************
    public interface OnLoadingListener {
        void onClick();
    }

}
